package com.hwpt.chesedemo;

import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

@RunWith(AndroidJUnit4.class)
public class QianwenInstrumentedTest {
    private static final String TAG = "QianwenInstrumentedTest";
    private static final String API_KEY = "sk-c0f76c2c02104d8e8590bbcdc1764db5";

    @Test
    public void testSyncCall() {
        try {
            // 初始化API Key
            System.setProperty("dashscope.api.key", API_KEY);

            List<Message> messages = new ArrayList<>();
            messages.add(Message.builder()
                    .role("system")
                    .content("你是一个简洁的回答助手，只说一句话")
                    .build());
            messages.add(Message.builder()
                    .role("user")
                    .content("Android的四大组件是什么？")
                    .build());

            GenerationParam param = GenerationParam.builder()
                    .model("qwen-turbo")
                    .messages(messages)
                    .build();

            Generation generation = new Generation();
            GenerationResult result = generation.call(param);

            if (result.getOutput() != null) {
                Log.d(TAG, "同步调用成功：" + result.getOutput().getText());
                System.out.println("大模型响应: " + result.getOutput().getText());
            } else {
                Log.e(TAG, "同步调用失败：" + result);
            }

        } catch (Exception e) {
            Log.e(TAG, "调用异常：" + e.getMessage());
            e.printStackTrace();
        }
    }
}